package com.luohoufa.ms.product.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket productApi() {
        final List<ResponseMessage> globalResponses = Arrays.asList(
                new ResponseMessageBuilder()
                        .code(200)
                        .message("OK")
                        .build(),
                new ResponseMessageBuilder()
                        .code(400)
                        .message("Bad Request")
                        .build(),
                new ResponseMessageBuilder()
                        .code(500)
                        .message("Internal Error")
                        .build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("产品服务接口文件")
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.luohoufa.ms.product.controller"))
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                //.paths(paths())
                .build()
                .apiInfo(apiInfo())
                .globalOperationParameters(setHeaderToken())
//                .globalResponseMessage(RequestMethod.GET, globalResponses)
//                .globalResponseMessage(RequestMethod.POST, globalResponses)
                ;
    }

    private List<Parameter> setHeaderToken() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("token")
                .description("接口访问token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        List<Parameter> parameterList = new ArrayList<>();
        parameterList.add(parameterBuilder.build());
        return parameterList;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("产品服务子系统")
                .description("产品服务子系统API服务，用于对接外部接口")
                .termsOfServiceUrl("no terms of service")
                .version("1.0")
                .build();
    }

    private Predicate<String> paths() {
        return Predicates.or(
                PathSelectors.regex("/business.*"),
                PathSelectors.regex("/some.*")
        );
    }

}
